package com.rla.controller;

import java.util.List;
import java.util.Arrays;

import com.rla.common.annotation.Anonymous;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.common.enums.BusinessType;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.domain.vo.BaseDrugTypeVo;
import com.rla.domain.bo.BaseDrugTypeBo;
import com.rla.service.IBaseDrugTypeService;
import com.rla.common.core.page.TableDataInfo;

/**
 * 商品分类
 *
 * @author rla
 * @date 2025-08-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drug/type")
public class BaseDrugTypeController extends BaseController {

    private final IBaseDrugTypeService iBaseDrugTypeService;

    /**
     * 查询商品分类列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo<BaseDrugTypeVo> list(BaseDrugTypeBo bo, PageQuery pageQuery) {
        return iBaseDrugTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品分类列表
     */
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseDrugTypeBo bo, HttpServletResponse response) {
        List<BaseDrugTypeVo> list = iBaseDrugTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品分类", BaseDrugTypeVo.class, response);
    }

    /**
     * 获取商品分类详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BaseDrugTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBaseDrugTypeService.queryById(id));
    }

    /**
     * 新增商品分类
     */
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseDrugTypeBo bo) {
        return toAjax(iBaseDrugTypeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品分类
     */
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseDrugTypeBo bo) {
        return toAjax(iBaseDrugTypeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品分类
     *
     * @param ids 主键串
     */
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBaseDrugTypeService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
